
// Depends
var path              = require('path');
var webpack           = require('webpack');
var autoprefixer      = require('autoprefixer-core');
var Manifest          = require('manifest-revision-webpack-plugin');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var HtmlWebpackPlugin = require("html-webpack-plugin");

var NODE_ENV = process.env.NODE_ENV || "production";
var SERVER_ENV = process.env.SERVER_ENV || "test";
var DEVELOPMENT  = NODE_ENV === "production" ? false : true;

var SERVER_CONTEXT = DEVELOPMENT ? '/com.fable.platform' : '/com.fable.platform';
var SERVER_IP =
  DEVELOPMENT ?
    'http://192.168.80.188:8080' :
      SERVER_ENV == 'test' ?
        'http://192.168.80.188:8080' : 'http://114.215.209.60:8080';
var QR_CODE_IP =
  DEVELOPMENT ? 'http://192.168.80.188' :
    SERVER_ENV == 'test' ?
      'http://192.168.80.188' : 'http://114.215.209.60';

console.log('SERVER_IP:' + SERVER_IP);
console.log('QR_CODE_IP:' + QR_CODE_IP);
// // tangbb
// var SERVER_CONTEXT = DEVELOPMENT ? '/com.fablezj.platform' : '/com.fable.platform';
// var SERVER_IP = DEVELOPMENT ? 'http://192.168.30.188:8080' : 'http://114.215.209.60:8080';
// var QR_CODE_IP = DEVELOPMENT ? 'http://192.168.30.188' : 'http://114.215.209.60';

var stylesLoader = 'css-loader?sourceMap!postcss-loader!sass-loader?outputStyle=expanded&sourceMap=true&sourceMapContents=true';

module.exports = function(_path) {
  var rootAssetPath = _path + 'src';

  var webpackConfig = {
    // entry points
    entry: {
      vendor: _path + '/src/app/index.vendor.js',
      app: _path + '/src/app/index.bootstrap.js'
    },

    // output system
    output: {
      path: 'dist',
      filename: '[name].entry.js',
      // publicPath: '/'
    },

    // resolves modules
    resolve: {
      extensions: ['', '.js'],
      modulesDirectories: ['node_modules'],
      alias: {
        _root:        path.join(_path, 'src'),
        _images:      path.join(_path, 'src', 'assets', 'images'),
        _appRoot:     path.join(_path, 'src', 'app'),
        _images:      path.join(_path, 'src', 'app', 'assets', 'images'),
        _stylesheets: path.join(_path, 'src', 'app', 'assets', 'styles'),
        _scripts:     path.join(_path, 'src', 'app', 'assets', 'js')
      }
    },

    // modules resolvers
    module: {
      noParse: [],
      loaders: [{
        test: /\.html$/,
        exclude: [/dialogs/],
        loaders: [
          'ngtemplate-loader?relativeTo='+ _path,
          'html-loader?attrs[]=img:src&attrs[]=img:data-src&attrs[]=img:ng-src'
        ]
      }, {
        test: /\.html$/,
        include: [/dialogs/],
        loaders: ['html-loader']
      },{
        test: /\.js$/,
        exclude: [/node_modules/],
        loaders: [
          // 'baggage-loader?[file].html&[file].css',
          'ng-annotate-loader',
          'babel-loader'
        ]
      }, {
        test: /\.css/,
        loaders: [
          'style-loader',
          'css-loader?sourceMap',
          'postcss-loader'
        ]
        // loader: ExtractTextPlugin.extract('style-loader','css-loader?sourceMap!postcss-loader')
      }, {
        test: /\.(scss|sass)$/,
        // loader: DEVELOPMENT ? ('style-loader!' + stylesLoader) : ExtractTextPlugin.extract('style-loader', stylesLoader)
        loader: ExtractTextPlugin.extract('style-loader', stylesLoader)
        // loaders: ["style-loader","css-loader?sourceMap","postcss-loader","sass-loader"]
        // loader: ('style-loader!' + stylesLoader)

      }, {
        test: /\.(woff2|woff|ttf|eot|svg)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
        loaders: [
          "url-loader?name=assets/fonts/[name]_[hash].[ext]"
        ]
      }, {
        test: /\.(jpe?g|png|gif)$/i,
        loaders: [
          'url-loader?name=assets/images/[name].[ext]?[hash]&limit=120000'
        ]
      }, {
        test: require.resolve("angular"),
        loaders: [
          "expose?angular"
        ]
      },
      {
        test: require.resolve("jquery"),
        loaders: [
          "expose?$",
          "expose?jQuery"
        ]
      },
      {
        test: require.resolve("webuploader"),
        loaders: [
          "expose?WebUploader"
        ]
      }
      
      ]
    },

    // externals: {
    //   'WebUploader' : 'webUploader',
    // },

    // post css
    postcss: [autoprefixer({ browsers: ['last 5 versions'] })],

    // load plugins
    plugins: [
      //new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /en|hu/),
      new webpack.ProvidePlugin({
        // $: 'jquery',
        // jQuery: 'jquery',
        // 'WebUploader' : 'webuploader',
      }),
      new webpack.DefinePlugin({
        'NODE_ENV': JSON.stringify(NODE_ENV),
        'SERVER_CONTEXT': JSON.stringify(SERVER_CONTEXT),
        'SERVER_IP': JSON.stringify(SERVER_IP),
        'QR_CODE_IP': JSON.stringify(QR_CODE_IP),
      }),
      new webpack.NoErrorsPlugin(),
      new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
      new webpack.optimize.DedupePlugin(),
      new webpack.optimize.AggressiveMergingPlugin({
        moveToParents: true
      }),
      new webpack.optimize.CommonsChunkPlugin({
        name: 'common',
        async: true,
        children: true,
        minChunks: Infinity
      }),
      // new Manifest(path.join(_path + '/config', 'manifest.json'), {
      //   rootAssetPath: rootAssetPath,
      //   ignorePaths: ['.DS_Store']
      // }),
      // new ExtractTextPlugin('assets/styles/css/[name]' + (NODE_ENV === 'development' ? '' : '.[chunkhash]') + '.css', { allChunks: true }),
      new ExtractTextPlugin('style.css'),
      new HtmlWebpackPlugin({
        filename: 'index.html',
        template: path.join(_path, 'src', 'tpl-index.html')
      })
    ]
  };

  if (NODE_ENV !== 'development') {
    webpackConfig.plugins = webpackConfig.plugins.concat([
      new webpack.optimize.UglifyJsPlugin({
        minimize: true,
        warnings: false,
        sourceMap: true
      })
    ]);
  }

  return webpackConfig;

};
